﻿/*****
 *  Copyright 2011, Kaven Gagné
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

using System;
using System.Reflection;

namespace Knowledge
{
    public static class Utilities
    {
        public enum errCode
        {
            NO_ERROR = 0,
            FILE_DONT_EXIST = -1,
            EXCEPTION = -2,
            NO_ROW_AFFECTED = -3,
            FOREIGN_KEY_CONSTRAINT = -4
        }


        #region ERRORS HANDLING
        public static void PrintError(string text)
        {
            Console.Error.WriteLine(text);
        }
        public static void PrintError(Exception e)
        {
            Console.Error.WriteLine("{0}\n{1}", e.Message, e.StackTrace);
        }
        #endregion //ERRORS HANDLING


        #region ATTRIBUTES
        /// <summary>
        /// Print Attributes of the given Object
        /// </summary>
        /// <param name="obj">Object for which to query Attributes</param>
        public static void PrintAttributes(Type type)
        {
            MemberInfo member = type;
            DumpAttributes(member);
            foreach (MethodInfo method in type.GetMethods())
            {
                if (IsMemberTested(method))
                {
                    Console.WriteLine("Member {0} is tested!", method.Name);
                }
                else
                {
                    Console.WriteLine("Member {0} is NOT tested!", method.Name);
                }
            }
        }

        private static void DumpAttributes(MemberInfo member)
        {
            Console.WriteLine("Attributes for : " + member.Name);
            foreach (object attribute in member.GetCustomAttributes(true))
            {
                Console.WriteLine(attribute);
            }
        }

        private static bool IsMemberTested(MemberInfo member)
        {
            foreach (object attribute in member.GetCustomAttributes(true))
            {
                if (attribute is IsTestedAttribute)
                {
                    return true;
                }
            }
            return false;
        } 
        #endregion //ATTRIBUTES
    }
}
